<?php
/**
 * 
 * @todo description of this class
 * 
 * @author Alton Crossley <crossleyframework@nogahidebootstrap.com>
 * @package Crossley Framework
 *  
 * @copyright Copyright (c) 2003-2009, Nogaide BootStrap INC. All rights reserved.
 * @license BSD http://opensource.org/licenses/bsd-license.php
 * @version $Id:$
 * 
 */
/**
 * 
 * Basic string functionality utility
 * 
 * 
 */
class X_String
{
    /**
     * format a stirng in camel case
     * @example X_String::toCapWords('a great framework'); <- returns 'AGreatFramework'
     * @example X_String::toCapWords('names_list'); <- returns 'NamesList'
     *
     * @param string $sString
     * @return string
     */
    static public function toCapWords($xString)
    {
        return str_replace(' ', '', self::toTitle($xString)); 
    }
    
    /**
     * turn a string into a title, also removes underscores
     * @example X_String::toTitle('a great framework'); <- returns 'A Great Framework'
     * @example X_String::toTitle('names_list'); <- returns 'Names List'
     *
     * @param string $sString
     * @return string
     */
    static public function toTitle($xString)
    {
        // handle array pieces
        if (is_array($xString))
        {
            $xString = implode('_', $xString);
        }
        
        $xString = str_replace('_', ' ', strtolower($xString));
        $xString = ucwords($xString);
        
        /**
         * @todo maybe handle title array('and', 'but', 'by', 'with', 'to', 'of', 'a', 'an', 'the');
         */
        
        return $xString;
    }
    
    /**
     * processes a string replacing valid urls with a text link
     * the text of the link is specified by $sLinkName
     *
     * @param string $sString
     * @param string $sLinkName
     * @return string
     */
    static public function makeLinks($sString, $sLinkName = 'link')
    {
        $sPattern = "([A-Za-z][A-Za-z0-9+.-]{1,120}:[A-Za-z0-9/](([A-Za-z0-9\$_.+!*,;/?:@&~=-])|%[A-Fa-f0-9]{2}){1,333}(#([a-zA-Z0-9][a-zA-Z0-9\$_.+!*,;/?:@&~=%-]{0,1000}))?)";
        $aMatches = array();
        
        preg_match_all($sPattern, $sString, $aMatches);
        
        foreach ($aMatches[0] as $sUrl)
        {
            self::stripPunctuation($sUrl);
            $sLink = sprintf('<a href="%s">%s</a>', $sUrl, $sLinkName);
            $sString = str_replace($sUrl, $sLink, $sString);
        }
        
        return $sString;        
    }
    
    static public function stripPunctuation(&$sString)
    {
        $aPunctuation = array('.', '!', '?');
        $cLast = $sString[strlen($sString)-1];
        if (in_array($cLast, $aPunctuation))
        {
            $sString = substr($sString, 0, -1);
        }
        
        return $sString;
    }
    
    /**
     * creates a class file path from a class name
     *
     * @param string $sClassName
     * @return string
     */
    static public function makeClassPath($sClassName)
    {
        return str_replace('_', DIRECTORY_SEPARATOR, $sClassName).'.php';
    }
    /**
     * filter a string and remove spaces
     *
     * @param string $sString
     * @return string
     */
    static public function toDomSafe($sString)
    {
        $sString = str_replace(' ', '_', $sString);
        return X_Filter_Alnum::go($sString, false, array('_', '-'));
    }
    /**
     * make a string a universally safe code consisting of just 
     * letters, numbers and underscores
     *
     * @param string $sString
     * @param bool $bToLower true
     * @return string
     */
    static public function toCode($sString, $bToLower = true)
    {
        $sString = self::toDomSafe(str_replace('-', '_', $sString));
        if ($bToLower)
        {
            $sString = strtolower($sString);
        }
        return $sString;
    }
    
    static public function toRealpath($sString)
    {
    	$aPath = explode('/', str_replace('\\', '/', $sString));
    	
    	if (count($aPath) < 1) return $sString;
    	
    	if ($aPath[0] == '.')
    	{
    		$aPath[0] =  str_replace('\\', '/', getcwd());
    	}
    	
    	foreach ($aPath as $iKey => $sValue)
    	{
    		if ($sValue == '..')
    		{
    			$iKeyToDelete = $iKey - 1;
    			if (array_key_exists($iKeyToDelete, $aPath)) unset($aPath[$iKeyToDelete]);
    			unset($aPath[$iKey]);
    		}
    	}
    	
    	return realpath(implode('/', $aPath));
    }
}